var uinfo;
function UserCenter_Code(info) {
	uinfo=info;
	var buffer = new StringBuffer();
	buffer.append('<div id="content" class="content" style="margin-top:0px; padding-top:15px;  background-color:#f3f3f3; height:'+(sheight-45)+'px;">');
	buffer.append('<ul class="usercenter">');
	buffer.append('<li id="headimg_tool" style="height:80px;"><div class="quan border-top-bottom" style="height:80px;"><div class="icon" style="width:80px;height:80px;"><img id="headimg" class="headimg" src="'+info.HeadImg+'" style="width:60px; height:60px;"></div><div class="name" style="width:65%;"><label class="realname">'+info.RealName+'</label><font>（'+info.RoleName+'）</font><p><p style="height:20px; overflow:hidden;">'+info.DepartName+'</p></div><div class="arrow" style="padding-top:30px; width:15px; padding-right: 30px;"><img src="img/arrow.png"/></div></div></li>');
	
	buffer.append('<li style="height:1px; margin-top:20px;"><div class="quan  border-bottom" style="width:100%; float:left; height:1px"></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">手机<font style="font-size:12px;">&nbsp; 账号</font></div><div class="text"><input id="txt_username" type="tel" maxlength="11" value="'+info.UserName+'"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">真实姓名</div><div class="text"><input id="txt_realname" type="text" value="'+info.RealName+'"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">性别</div><div class="arrow"><img src="img/arrow.png"/></div><div class="text" style="width:200px; float:right;"><select id="txt_sex"><option value="男">男</option><option value="女">女</option></select></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">籍贯</div><div class="text"><input id="txt_birthplace" type="text" value="'+info.BirthPlace+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">现住址</div><div class="text"><input id="txt_address" type="text" value="'+info.Address+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom dates" style="width:96%;float:right"><div class="name" style="padding-left:0px;">生日</div><div class="text"><input id="txt_birthday" type="text" readonly="readonly" value="'+info.Birthday.ToDate()+'"/></div><div class="arrow"><img src="img/arrow.png"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">固话</div><div class="text"><input id="txt_tel" type="tel" value="'+info.Tel+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px; width:50%;">入职时间<font style="font-size:12px;">&nbsp; 不能修改</font></div><div class="text" style="width:44%;"><input id="txt_joindate" type="text" readonly="readonly" value="'+info.JoinDate.ToDate()+'"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">邮箱</div><div class="text"><input id="txt_email" type="text" value="'+info.Email+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">QQ</div><div class="text"><input id="txt_qq" type="tel" value="'+info.QQ+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">毕业院校</div><div class="text"><input id="txt_school" type="text" value="'+info.School+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom dates" style="width:96%;float:right"><div class="name" style="padding-left:0px;">毕业时间</div><div class="text"><input id="txt_schooldate" type="text" readonly="readonly" value="'+info.SchoolDate.ToDate()+'"/></div><div class="arrow"><img src="img/arrow.png"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:96%;float:right"><div class="name" style="padding-left:0px;">教育类型</div><div class="text"><input id="txt_schooltype" type="text" value="'+info.SchoolType+'" onfocus="ScrollTop(1)"/></div></div></li>');
	buffer.append('<li><div class="quan border-bottom" style="width:100%;float:right"><div class="name" style="padding-left:4%">专业</div><div class="text"><input id="txt_profession" type="text" value="'+info.Profession+'" onfocus="ScrollTop(1)"/></div></div></li>'); 
	buffer.append('</ul>');
	buffer.append('</div>');
	buffer.append(CutImg_Code());
	return buffer.toString();
};

function CutImg_Code(){
	var buffer = new StringBuffer();
		buffer.append('<div id="showcutimg" style="width:100%; height:100%; background-color:#fff; position:absolute; display:none;">');
			buffer.append('<div id="showEdit" style="width:100%;  z-index:100; position:absolute;">');
				buffer.append('<div style="width: 100%;">');
					buffer.append('<div id="report" style="width: 100%; position:absolute;">');
					buffer.append('</div>');
					buffer.append('<div style="margin-top:30px;width: 100%;height: 60px;line-height: 60px!important;position: fixed;bottom: 0px;text-align: center;background-color: rgba(255,255,255,0.5);padding-top: 10px;">');
						buffer.append('<div style="width: 100%;height: 40px;">');
							buffer.append('<button id="picback" type="button" class="mui-btn mui-btn-danger" style="width: 46%;height: 40px;" onclick="closecut();">取消</button> &nbsp;');
							buffer.append('<button id="picsave" type="button" class="mui-btn mui-btn-danger" style="width: 46%;height: 40px;" onclick="confirm();">保存</button>');
						buffer.append('</div>');
					buffer.append('</div>');
				buffer.append('</div>');
			buffer.append('</div>');
		buffer.append('</div>');
	return buffer.toString();
};

function Init_UserCenter()
{
		document.getElementById("report").style.height = (document.documentElement.clientHeight - 105) + "px";
		mui(".usercenter").on("tap",".dates",function(){
			obj=this;
			var t=new Date();
			if($(this).find("input").val()!="")
			{
				var year_month_day=$(this).find("input").val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]); 
			}
			plus.nativeUI.pickDate( function(e){
				//在ios上， 1988-9-11 和 1991年9月15等日期的日期都会小一天，所以要加上1，原因待查
				/*
				var d=e.date;
				if(plus.device.vendor=="Apple" && DateDiff("M",d.Format("yyyy-MM-dd"),'1991-09-15')>0)
    			{
					d=new Date((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).replace(/-/g, "/"));
					d.setDate(d.getDate()+1);
				}
				$(obj).find("input").val(d.Format("yyyy-MM-dd"));
				*/
				var d=e.date;
				$(obj).find("input").val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
		});
		
		
		get("headimg_tool").addEventListener('tap', function(event) {
				//get("share_context").blur();
						var btnArray = [{
							title: "拍照"
						}, {
							title: "从相册选择"
						}];
						plus.nativeUI.actionSheet({
							title: "选择照片",
							cancel: "取消",
							buttons: btnArray
						}, function(e) {
							var index = e.index;
							switch (index) {
								case 0:
									break; 
								case 1:
									var cmr = plus.camera.getCamera();
									cmr.captureImage(function(path) {
										compressImage("file://" + plus.io.convertLocalFileSystemURL(path));
									}, function(err) {});
									break;
								case 2:
									
									plus.gallery.pick(function(path) {
									    	compressImage(path);
									}, function(err) {}, {filter:"image"});
									break;
							}
						}); 
					}, false);

};

function Update_User()
{
	var data = {};
        var txt_username = $("#txt_username");
        var txt_userpwd = $("#txt_userpwd");
        var txt_realname = $("#txt_realname");
        var sex = $("#txt_sex");
        data.id = uinfo.ID;
        if(txt_username.val().Trim()=="")
        {
        	plus.nativeUI.toast("请输入账号");
        	return false;
        }
        if(txt_realname.val().Trim()=="")
        {
        	plus.nativeUI.toast("请输入真实姓名");
        	return false;
        }
		data.act = "Save_User";
		data.username = txt_username.val();
        data.userpwd = uinfo.UserPwd;
        
        data.realname = txt_realname.val();
        data.sex = sex.val();
        data.departid = uinfo.DepartID;
        data.roleid = uinfo.RoleID;
        data.joindate = $("#txt_joindate").val();
        data.birthday = $("#txt_birthday").val();
        data.tel = $("#txt_tel").val();
        data.idcard = uinfo.IDCard;
        data.birthplace = $("#txt_birthplace").val();
        data.address = $("#txt_address").val();
        data.email = $("#txt_email").val();
        data.qq = $("#txt_qq").val();
        data.school = $("#txt_school").val();
        data.schooldate = $("#txt_schooldate").val();
        data.schooltype = $("#txt_schooltype").val();
        data.profession = $("#txt_profession").val();
        data.userid=userid;
		data.comid=comid;
        data.source = source; 
        data.version = version;
        data.sign=GetSign(data.act);
        mui.getJSON(admin_domain, data, function (json) {
        	plus.nativeUI.toast(json.msg);
            if (json.state) {
                User.RealName=data.realname;
                $(".realname").html(data.realname);
            } else {
				
            }
        });
};

			/**
			 * 声明对象
			 * @param {Object} value
			 * @param {Object} text
			 */
			function objData(value, text) {
				this.value = value;
				this.text = text;
			}
			
			
			/**
			 * 压缩图片
			 * @param {Object} imgpath
			 * @param {Object} tempname
			 */
			function compressImage(imgpath) {
				$("#divmsg1 .back,#divmsg1 .tool").css("visibility","hidden");
				$("#divmsg1 .title .name").html("修改头像");
				// 创建一个image对象
				var imgtemp = new Image();
				// 给临时图片赋值
				imgtemp.src = imgpath;
				// 图片加载完成后事件
				imgtemp.onload = function() {
					// 图片真实宽度
					var tmpw = this.width;
					// 图片真实高度
					var tmph = this.height;
					// 计算长宽比
					if (tmpw > 1000 || tmph > 1000) {
						if (tmpw >= tmph) {
							tmpw = 1000;
						} else {
							tmpw = 1000 * tmpw / tmph;
						}
					}
					tmpw = tmpw/2 + "px";
					tmph = tmph/2 + "px";
					// 执行图片压缩事件
					var wt = plus.nativeUI.showWaiting("请稍后...");
					$('#hdiv').hide();
					var img=imgpath.substring(imgpath.lastIndexOf("/")+1,imgpath.length).split(".")[0];
					plus.zip.compressImage({
							src: imgtemp.src, // 源文件，绝对文件地址
							dst: "_doc/" +img+'_o.jpg', // 目标文件路径，绝对地址或者相对地址
							quality: 80, // 图片质量：0-100
							width: tmpw, // 图片宽度
							format: "jpg", // 图片类型
							overwrite: true, // 是否覆盖同名图片
						},
						function(i) {
							// 添加图片
							
							$("#report").html("<img src='" + eval(i).target + "?version=" + new Date().getTime() + "' >");
							$("#showcutimg").fadeIn();
							cutImg();
							setTimeout(function() {
								wt.close();
							}, 500);
						},
						function(e) {});
				};
			}
			/**
			 * 照片裁剪类
			 */
			function cutImg() {
				var image = $('#report > img');
				image.cropper({
					aspectRatio: 1 / 1,
					autoCropArea: 0.6,
					resizable: false,
					zoomable: true,
					guides: false,
					background: false,
					highlight: true,
					movable: false,
					dragCrop: false
				});
			};
			/**
			 * 保存并确定裁剪
			 */
			function confirm() {
				$("#divmsg1 .back,#divmsg1 .tool").css("visibility","visible");
				$("#divmsg1 .title .name").html("我的资料");
				var wt = plus.nativeUI.showWaiting("请稍后...");
				$("#showcutimg").hide();
				var image = $('#report > img');
				var dataURL = convertCanvasToImage(image.cropper("getCroppedCanvas")).src;
				$(".headimg").attr("src", dataURL);
				$('#hdiv').show();
				$("#report").html("");
				
				var headimg=$("#headimg").attr("src").replace(/^data:image\/(png|jpeg);base64,/, "");
				var data = {};
				data.act = "Update_HeadImg";
				data.userid=userid;
				data.comid=comid;
				data.headimg=headimg;
				data.source=source;
				data.sign=GetSign(data.act);
				mui.ajax(index_domain, {
					data: data,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(json) {
						wt.close();
						User.HeadImg=json.data;
						plus.nativeUI.toast("保存成功");
						
					}
				});
			};

			function convertCanvasToImage(canvas) {
				var image = new Image();
				image.src = canvas.toDataURL("image/jpeg", 0.8);
				return image;
			};
			/**
			 * 取消事件
			 */
			function closecut() {
				$("#divmsg1 .back,#divmsg1 .tool").css("visibility","visible");
				$("#divmsg1 .title .name").html("我的资料");
				$('#showcutimg').hide();
				$('#hdiv').show();
				$("#report").html("");
			}